#include <algorithm>
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        int n=nums.size();
        vector<vector<int>>ans;
        sort(nums.begin(),nums.end());
        for(int i=0;i<n-3;i++)
        {
            if(i&&nums[i]==nums[i-1])continue;
            for(int j=i+1;j<n-2;j++)
            {
                if(j!=i+1&&nums[j]==nums[j-1])continue;
                int l=j+1,r=n-1;
                for(;l<r;l++)
                {
                    if(l!=j+1&&nums[l]==nums[l-1])continue;
                    while(l<r&&((long)nums[i]+nums[j]+nums[l]+nums[r])>target)r--;
                    if(l<r&&((long)nums[i]+nums[j]+nums[l]+nums[r])==target)
                    ans.push_back({nums[i],nums[j],nums[l],nums[r]});
                    
                }
            }
        }
        return ans;
    }
};